use serde::Deserialize;

/// rabbitmq连接配置
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct AmqpConfig {
    /// 如果指定了url
    /// 则忽略 ssl、host、port、username、password、vhost的值
    url: Option<String>,
    /// 是否加密
    #[serde(default)]
    ssl: bool,
    /// ip
    host: Option<String>,
    /// 端口
    port: Option<u16>,
    /// 用户名
    username: Option<String>,
    /// 密码
    password: Option<String>,
    /// 虚拟地址
    vhost: Option<String>,
}

impl Default for AmqpConfig {
    fn default() -> Self {
        Self {
            url: None,
            ssl: false,
            host: None,
            port: None,
            username: None,
            password: None,
            vhost: None,
        }
    }
}


impl AmqpConfig {
    pub fn url(&self) -> Option<&String> {
        self.url.as_ref()
    }

    pub fn ssl(&self) -> bool {
        self.ssl
    }

    pub fn host(&self) -> &str {
        self.host.as_deref().unwrap_or("localhost")
    }

    pub fn port(&self) -> u16 {
        self.port.unwrap_or(5672)
    }

    pub fn username(&self) -> &str {
        self.username.as_deref().unwrap_or("")
    }

    pub fn password(&self) -> &str {
        self.password.as_deref().unwrap_or("")
    }

    pub fn vhost(&self) -> &str {
        self.vhost.as_deref().unwrap_or("%2f")
    }

}